package login;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final String CONENT_TYPE="text/html; charset=UTF-8";
    
//	初始化Servlet
    public void init() throws ServletException{
    	
    }
    
//    处理HTTP POST请求
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
//    	从请求中取出用户名和密码的值
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	
//    	生成一个ArrayList对象，并把用户名和密码的值存入该对象中
    	ArrayList arr =  new ArrayList();
    	arr.add(username);
    	arr.add(password);
    	
//    	生成一个session对象
    	HttpSession session = request.getSession(true);
    	session.removeAttribute("username");
    	session.setAttribute("username", username);
    	
//    	调用模型组件loginHandler， 检查该用户是否已注册
    	LoginHandler login = new LoginHandler();
    	boolean mark = login.checkLogin(arr);
    	
//    	如何已注册，进入页面
    	if(mark) response.sendRedirect("main.html");
//    	如果未注册，进入注册页面
    	else response.sendRedirect("register.jsp");	
    }
    
//    处理HTTPGET请求
    public void doGet(HttpServletRequest request,HttpServletResponse response)
    	throws ServletException, IOException{
    		doPost(request,response);
    	}

    
//    销毁Servlet
    	public void destroy(){
    		
    	}
    	

    

}
